package com.example.ticket.controller;

import com.example.ticket.bean.ResponseBean;
import com.example.ticket.entity.Ticket;
import com.example.ticket.entity.User;
import com.example.ticket.mapper.TicketMapper;
import com.example.ticket.mapper.UserMapper;
import com.example.ticket.service.TicketService;
import com.example.ticket.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/ticket")
public class TicketController {
    @Autowired
    TicketService ticketService;

    @Autowired
    UserService userService;

    @Autowired
    TicketMapper ticketMapper;

    //多条件查询
    @PostMapping("/search")
    public ResponseBean Search(@RequestBody Ticket ticket){

        List<Ticket> list=ticketMapper.selectALLBycondition(ticket);
        if(list!=null) {
            return ResponseBean.success(list);
        }else{
            return ResponseBean.notFound();
        }
    }

    //多条件查询个人票据
    @PostMapping("/searchmy")
    public ResponseBean SearchMy(@RequestBody Ticket ticket){
        SecurityContext context= SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        User user=userService.selectByUsername(principal.getUsername());
        ticket.setUserId(user.getId());
        List<Ticket> list=ticketMapper.selectMyBycondition(ticket);
        if(list!=null) {
            return ResponseBean.success(list);
        }else{
            return ResponseBean.notFound();
        }
    }

    //所有票据列表
    @GetMapping("/ticketlist")
    public ResponseBean findAllTicket(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,7);
        List<Ticket> list=ticketService.TicketList();
        if(list!=null){
            PageInfo<Ticket> ticketInfo = new PageInfo<Ticket>(list);

            return ResponseBean.success(ticketInfo);
        }else{
            return ResponseBean.notFound();
        }
    }

    //提交票据
    @PostMapping("/submit/{id}")
    public ResponseBean SubmitTicket(@PathVariable Integer id){
        //待审核状态
        Ticket ticket=ticketService.selectById(id);
        ticket.setStatus(2);
        if(ticketService.updateTicketStatus(ticket)!=null){
            return ResponseBean.success();
        }else{
            return ResponseBean.fail();
        }
    }

    //审核通过
    @PostMapping("/approved/{id}")
    public ResponseBean ApprovedTicket(@PathVariable Integer id){
        //审核通过状态
        Ticket ticket=ticketService.selectById(id);
        ticket.setStatus(1);
        ticket.setId(id);
        if(ticketService.updateTicketStatus(ticket)!=null){
            return ResponseBean.success();
        }else{
            return ResponseBean.fail();
        }
    }

    //审核不通过/退回
    @PostMapping("/back/{id}")
    public ResponseBean BackTicket(@PathVariable Integer id,String remark){
        //退回票据状态
        Ticket ticket=ticketService.selectById(id);
        ticket.setRemark(remark);
        ticket.setStatus(3);
        ticket.setId(id);
        if(ticketService.updateBack(ticket)!=null){
            return ResponseBean.success();
        }else{
            return ResponseBean.fail();
        }
    }

    //保存
    @PostMapping("/saveticket")
    public ResponseBean SaveTicket(@RequestBody Ticket ticket){
        SecurityContext context=SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        User user=userService.selectByUsername(principal.getUsername());
        ticket.setUserId(user.getId());
        //状态为0b保存/未提交
        ticket.setStatus(0);
        if (ticketService.insertTicket(ticket) != null) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }

    //提交
    @PostMapping("/submitticket")
    public ResponseBean SubmitTicket(@RequestBody Ticket ticket){
        SecurityContext context=SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        User user=userService.selectByUsername(principal.getUsername());
        ticket.setUserId(user.getId());
        //状态为0b保存/未提交
        ticket.setStatus(2);
        if (ticketService.insertTicket(ticket) != null) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }

    //ID删除票据
    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteTicketById(@PathVariable Integer id){
        if (ticketService.deleteById(id) != 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }

    //ID查看票据详情
    @GetMapping("/view/{id}")
    public ResponseBean selectTicketById(@PathVariable Integer id){

        Ticket ticket=ticketService.selectById(id);
        if (ticket == null) {
            return ResponseBean.notFound();
        } else {
            return ResponseBean.success(ticket);
        }
    }

    @PostMapping("/updatesave")
    public ResponseBean updateTicketSave(@RequestBody Ticket ticket){
        ticket.setStatus(0);
        if (ticketService.updateTicketById(ticket) != null) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }

    @PostMapping("/updatesubmit")
    public ResponseBean updateTicketSubmit(@RequestBody Ticket ticket){
        ticket.setStatus(2);
        ticket.setSubmitdate(new Date());
        if (ticketService.updateTicketById(ticket) != null) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }



    //所有自己票据列表
    @GetMapping("/mylist")
    public ResponseBean findmyTicket(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,7);
        SecurityContext context=SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        User user=userService.selectByUsername(principal.getUsername());
        List<Ticket> list=ticketService.selectMyTicket(user.getId());
        if(list!=null){
            PageInfo<Ticket> ticketInfo = new PageInfo<Ticket>(list);
            return ResponseBean.success(ticketInfo);
        }else{
            return ResponseBean.notFound();
        }
    }

/*
    //模糊查询票据
    @GetMapping("/lookfor")
    public ResponseBean LookforTicket(){
        List<Ticket> list=ticketService.selectBycondition("2",1);
        return ResponseBean.success(list);
    }
*/


    @GetMapping("/statuslist/{status}")
    public ResponseBean CheckTicket(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,@PathVariable Integer status){
        PageHelper.startPage(pageNum,7);
        List<Ticket> list=ticketService.selectSTticket(status);
        if(list!=null){
            PageInfo<Ticket> ticketInfo = new PageInfo<Ticket>(list);
            return ResponseBean.success(ticketInfo);
        }else{
            return ResponseBean.notFound();
        }
    }

        //未提交的票据
    @GetMapping("/mystlist/{status}")
    public ResponseBean findmySaveTicket(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,@PathVariable Integer status){
        PageHelper.startPage(pageNum,7);
        SecurityContext context=SecurityContextHolder.getContext();
        System.out.println("userDetails:"+context);
        Authentication authentication=context.getAuthentication();
        UserDetails principal=(UserDetails)authentication.getPrincipal();
        User user=userService.selectByUsername(principal.getUsername());
        List<Ticket> list=ticketService.selectMyTicketByStatus(status,user.getId());
        if(list!=null){
            PageInfo<Ticket> ticketInfo = new PageInfo<Ticket>(list);
            return ResponseBean.success(ticketInfo);
        }else{
            return ResponseBean.notFound();
        }
    }



}
